"use strict";

const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient
let db = null
exports.ObjectId = mongodb.ObjectId

const url = 'mongodb://localhost:27017/studentsystem'
MongoClient.connect(url, function (err, mydb) {
    if (err) console.log(err);
    db = mydb
})
exports.findOne = (fileName, params, callback) => {
    db.collection(fileName).findOne(params, (err, doc) => {
        if (err) {
            console.log(err)
            return
        }
        console.log("查询的结果")
        callback && callback(doc)
    })
}
exports.findMany = (fileName, params, callback) => {
    db.collection(fileName).find(params).toArray((err, doc) => {
        if (err) {
            console.log(err)
            return
        }
        callback && callback(doc)
    })
}
exports.insertOne = (fileName, params, callback) => {
    db.collection(fileName).insertOne(params, (err, result) => {
        console.log("新增列表")
        if (err) {
            callback && callback(false)
        } else {
            callback && callback(true)
        }
    })
}
exports.deleteOne = (fileName, params, callback) => {
    db.collection(fileName).deleteOne(params, (err, result) => {
        console.log("删除成功")
        if (err) {
            callback && callback(false)
        } else {
            callback && callback(true)
        }
    })
}
exports.uptateOne = (fileName, filter, update, callback) => {
    db.collection(fileName).updateOne(filter, {$set: update}, (err, data) => {
        if(err){
            callback && callback(false)
        }else {
            callback && callback(true)
        }
    })
}